$(function () {

    //begin列表信息
    $("#employeeNeeds_datagrid").datagrid({
        url: "/employeeNeeds/list",
        onClickRow:function (index,row) {
            if(row.recruitPlanId){
                $("#edit").linkbutton({text:'查看'});
                $("#remove").hide(1000);
                $("#save").hide(1000);
            }else{
                $("#edit").linkbutton({text:'编辑'});
                $("#remove").linkbutton({disabled:false});
                $("#remove").show(1000);
                $("#save").show(1000);
            }
        },
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "编号", width: 100, hidden: true},
            {field: "inputUser", title: "录入人", width: 100,formatter:function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "暂未查询";
                }
            }},
            {
                field: "department", title: "申请部门", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return "未分配";
                }
            }
            },
            {field: "applicant", title: "申请人", width: 100,formatter: function (value, row, index) {
                if (value) {
                    return value.username;
                } else {
                    return "暂未查询";
                }
            }},
            {field: "begindate", title: "开始时间", width: 100},
            {field: "enddate", title: "结束时间", width: 100},
            {field: "inputdate", title: "录入时间", width: 100},
            {field: "recruitPlanId", title: "状态", width: 100,formatter: function (value, row, index) {
                if (value) {
                    return '<font color="green">已审核</font>';
                } else {
                    return '<font color="red">未审核</font>';
                }
            }}
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber: 1,
        pageSize: 3,
        pageList: [3, 5, 10, 20],
        toolbar: "#employeeNeeds_tb"
    });
    //end列表信息
    //添加明细的按钮
    $("#employeeNeedsBill").datagrid({
        iconCls: 'icon-edit',
        singleSelect: true,
        toolbar: '#employeeNeedsBill_bb',
        method: 'get',
        onClickRow: onClickRow,
        columns: [[
            {field: "id", title: "编号", hidden: true},
            {
                field: "job", title: "职位", width: 100,
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: 'name',
                        textField: 'name',
                        method: 'get',
                        panelHeight:'auto',
                        url: '/dataDictionaryItem/selectByParentId?id=3',
                        required: true
                    }
                }
            },
            {field: "number", title: "人数", editor: 'numberbox', width: 100},
            {
                field: "recoed", title: "学历", width: 120,
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: 'name',
                        panelHeight:'auto',
                        textField: 'name',
                        method: 'get',
                        url: '/dataDictionaryItem/selectByParentId?id=2',
                        required: true,
                        on: '无学历', off: ''
                    }
                }
            },
            {field: "skil", editor: 'textbox', title: "技能", width: 120},
            {field: "note", editor: 'textbox', title: "备注", width: 120}
        ]]
    });


    //begin添加/编辑对话框
    $("#employeeNeeds_dialog").dialog({
        width: 700,
        height: 350,
        buttons: "#employeeNeeds_bb",
        closed: true
    });
    //end添加/编辑对话框


    //begin处理功能方法的对象
    var obj = {
            //begin添加按钮
            add: function () {
                var rows = $('#employeeNeedsBill').datagrid('getRows');
                for (var i=rows.length;i>0;i--){
                    $('#employeeNeedsBill').datagrid('deleteRow',i-1);
                }
                $("#employeeNeeds_form").form("clear");
                $("#employeeNeedsBill_form").form("clear");
                $("#employeeNeeds_dialog").dialog("setTitle", "用人需求添加");
                $("#employeeNeeds_dialog").dialog("open");
            },
            //end添加按钮

//begin编辑按钮
            edit: function () {
                var rows = $('#employeeNeedsBill').datagrid('getRows');
                for (var i=rows.length;i>0;i--){
                    $('#employeeNeedsBill').datagrid('deleteRow',i-1);
                }
                // $("#employeeNeedsBill").datagrid("selectAll");
               // $("#employeeNeedsBill").datagrid("clearSelections");
                $("#employeeNeeds_form").form("clear");
                $("#employeeNeedsBill_form").form("clear");
                $("#employeeNeeds_dialog").dialog("setTitle", "用人需求编辑");
                var row = $("#employeeNeeds_datagrid").datagrid("getSelected");


                if (!row) {
                    $.messager.alert('温馨提示', '请选择要操作的行!');
                    return;
                }
                if (row.department) {
                    row["department.id"] = row.department.id;
                }
                var employeeNeedsId = row.id;
                $.get("/employeeNeedsBill/selectEmployeeNeedsBillByEmployeeNeedsId",
                    {employeeNeedsId: employeeNeedsId},
                    function (data) {
                        for(var i=0;i<data.length;i++){
                            var job=data[i].job;
                            var number=data[i].number;
                            var recoed=data[i].recoed;
                            var skil=data[i].skil;
                            var note=data[i].note;
                            $("#employeeNeedsBill").datagrid('insertRow',{
                                index:i,
                                row: {
                                    job:job,
                                    number:number,
                                    recoed: recoed,
                                    skil: skil,
                                    note: note
                                }
                            });
                        }
                    });
                //employeeNeedsBill表格属性设置
                $("#employeeNeeds_form").form("load",row);
                $("#employeeNeeds_dialog").dialog("open");
            },
//end编辑按钮


            //begin取消按钮
            cancel: function () {
                $("#employeeNeeds_dialog").dialog("close");
            },
            //end取消按钮

            //begin保存按钮
            save: function () {
                var employeeNeedsId = $("#employeeNeedsId").val();
                var url;
                if (employeeNeedsId) {
                    url = "/employeeNeeds/update"
                } else {
                    url = "/employeeNeeds/save"
                }
                $("#employeeNeeds_form").form("submit", {
                        url: url,
                        onSubmit: function (param) {
                            $('#employeeNeedsBill').datagrid('acceptChanges');
                            //先获取到role这个框里面所有的值,在遍历出来形成roles[index]
                            var rows = $("#employeeNeedsBill").datagrid("getRows");
                            console.log(rows);
                            for (var i = 0; i < rows.length; i++) {
                                console.log(rows[i]);
                                param["items[" + i + "].job"] = rows[i].job;
                                param["items[" + i + "].skil"] = rows[i].skil;
                                param["items[" + i + "].number"] = rows[i].number;
                                param["items[" + i + "].recoed"] = rows[i].recoed;
                                param["items[" + i + "].note"] = rows[i].note;
                            }
                        },
                        success: function (data) {
                            var data = $.parseJSON(data);
                            if (!data.success) {
                                $.messager.alert('温馨提示', data.msg);
                            } else {
                                $.messager.alert('温馨提示', "操作成功!");
                                $("#employeeNeedsBill").datagrid("selectAll");
                                $("#employeeNeedsBill").datagrid("clearSelections");
                                $("#employeeNeeds_dialog").dialog("close");
                                $("#employeeNeeds_datagrid").datagrid("reload");
                            }
                        }
                    }
                )
                ;
            },
            //end保存按钮

            //begin删除按钮
            remove: function () {
                var row = $("#employeeNeeds_datagrid").datagrid("getSelected");
                if (!row) {
                    $.messager.alert('温馨提示', '请选择要操作的行!');
                    return;
                }
                $.messager.confirm('确认', '您确认要操作此行吗？', function (r) {
                    if (r) {
                        //注意这里的id要从当前选中的行中获取
                        var employeeNeedsId = row.id;
                        $.get("/employeeNeeds/delete", {employeeNeedsId: employeeNeedsId}, function (data) {
                            if (!data.success) {
                                $.messager.alert('温馨提示', data.msg);
                            } else {
                                $.messager.alert('温馨提示', '操作成功!');
                                $("#employeeNeeds_datagrid").datagrid("reload");
                            }
                        });
                    }
                });

            },
            //end删除按钮

            // begin 高级查询按钮
            query: function () {
                var beginDate = $("#beginDate").datebox("getValue");
                var departmentId = $("#departmentId").datebox("getValue");
                var endDate = $("#endDate").datebox("getValue");
                $("#employeeNeeds_datagrid").datagrid("load", {
                    beginDate: beginDate,
                    endDate: endDate,
                    departmentId:departmentId
                })
            },
            // end 高级查询
            //begin刷新按钮
            reload: function () {
                $("#employeeNeeds_datagrid").datagrid("reload")
            },
            //end刷新按钮


            // 添加一行
            append: function () {
                if (endEditing()) {
                    //如果之前没有选中任何列,就直接添加
                    $('#employeeNeedsBill').datagrid('appendRow', {recoed: '无学历'});
                    //获取最后一个行的index
                    editIndex = $('#employeeNeedsBill').datagrid('getRows').length - 1;
                    //从最后一行开始编辑
                    $('#employeeNeedsBill').datagrid('selectRow', editIndex)
                        .datagrid('beginEdit', editIndex);
                }
            },
            removeit: function () {
                if (editIndex == undefined) {
                    return
                }
                $('#employeeNeedsBill').datagrid('cancelEdit', editIndex)
                    .datagrid('deleteRow', editIndex);
                editIndex = undefined;
            },


            reject: function () {
                $('#employeeNeedsBill').datagrid('rejectChanges');
                editIndex = undefined;
            }

        }
    ;
    //end处理功能方法的对象





    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

    // datagrid行点击事件
    function onClickRow(index, row) {
        if (editIndex != index) {
            if (endEditing()) {
                //选择选中的这一行开始编辑
                $("#employeeNeedsBill").datagrid("selectRow", index).datagrid("beginEdit", index);
                //吧这个的index赋值给editIndex
                editIndex = index;
            } else {
                $("#employeeNeedsBill").datagrid("selectRow", editIndex);
            }
        }
    }

});


var editIndex;
function endEditing() {
    if (!editIndex) {
        return true
    }
    //验证这一行,有效时返回true
    if ($('#employeeNeedsBill').datagrid('validateRow', editIndex)) {
        //结束编辑这一节点
        $('#employeeNeedsBill').datagrid('endEdit', editIndex);
        editIndex = null;
        return true;
    } else {
        return false;
    }
}

//begin表格行的点击事件
$("#employeeNeeds_datagrid").datagrid("onClickRow",function (index,row) {
    if(row.recruitPlanId){
        $("#edit").linkbutton({text:'查看'});
        $("#remove").linkbutton({disabled:true});
        $("#employeeNeeds_form").attr('readonly',true);
        $("#employeeNeedsBill_form").attr('readonly',true);
    }else{
        $("#edit").linkbutton({text:'编辑'});
        $("#remove").linkbutton({disabled:false});
        $("#employeeNeeds_form").attr('readonly',false);
        $("#employeeNeedsBill_form").attr('readonly',false);
    }
});
//end表格行的点击事件

